<?php
include_once("functions/db_manipulate.php");

if (!init()) {
  redirect("?action=login");
}

$dialogsResultSet = getDialogs();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=<?= getMessageByKey("default.charset") ?>"/>
  <title><?= getMessageByKey("dialogs.title") ?></title>
  <link rel="stylesheet" href="css/navigation.css">
  <link rel="stylesheet" type="text/css" href="css/menu.css">
  <link rel="stylesheet" type="text/css" href="css/common.css">
  <link rel="stylesheet" type="text/css" href="css/dialogs.css">
  <script src="resources/jquery.js"></script>
</head>

<body>
<?php
include_once("menu.php");
?>

<div class="maincommon">

  <div class="dialogsheader">
    <?= getMessageByKey('user.dialogs')?>
  </div>

  <form action="?action=searchu" accept-charset="UTF-8" method="post" id="userform">
    <div class="searchdiv">
      <?= getMessageByKey("find.user") ?>  <input type="text" maxlength="20" name="user">

      <div class="searchbutton" onclick="$('#userform').submit()">
        <?= getMessageByKey("button.general.find") ?>
      </div>
    </div>
  </form>

  <?php

  while ($dialog = mysql_fetch_assoc($dialogsResultSet)) {
    $lastMsgInfoSet = getDialogLastMsgInfo($dialog['id']);

    $lastMessageInfo = mysql_fetch_assoc($lastMsgInfoSet);

    $msg = countDialogMessages($dialog['id']);

    $light = "";

    if ($dialog['uread'] == 0 && $dialog['getter'] == $_SESSION['id']) {
      $light = "style=\"background-color: rgba(255, 127, 036, 1);\"";
    }
    ?>

    <div class="dialog">
      <div class="header" <?= $light ?> onclick="location.href='?action=conv&did=<?= $dialog['id'] ?>#lmsg'">
        <?= $dialog['user'] ?>
      </div>
      <div class="info">
        <div class="td">
          <?= getMessageByKey("general.date") ?> <?= $lastMessageInfo['date'] ?>
        </div>
        <div class="td">
          <?= getMessageByKey("general.time") ?> <?= $lastMessageInfo['time'] ?>
        </div>
        <div class="td">
          <?= getMessageByKey("dialogs.dialog.messages") ?> <?= $msg ?>
        </div>
      </div>
      <div class="buttonsdiv">
        <div class="button" onclick="$('#deletediv<?= $dialog['id'] ?>').slideDown('slow')">
          <?= getMessageByKey("button.general.delete") ?>
        </div>
      </div>
      <div class="askdelete" id="deletediv<?= $dialog['id'] ?>">

        <?= getMessageByKey("dialogs.dialog.drop.ask") ?> <br>

        <div class="buttonsdiv">
          <div class="button" onclick="location.href='?action=dialdel&did=<?= $dialog['id'] ?>'">
            <?= getMessageByKey("general.yes") ?>
          </div>
          <div class="button" onclick="$('#deletediv<?= $dialog['id'] ?>').slideUp('slow')">
            <?= getMessageByKey("button.general.cancel") ?>
          </div>
        </div>
      </div>
    </div>
  <?php
  }
  ?>

</div>

<?php
include_once("navigation.php");
?>
</body>
</html>
